<script setup>
import { computed } from 'vue';
//useCartStore：从 Pinia store 中导入，用于管理购物车的状态。
import { useCartStore } from '../stores/cart';

import { useRouter } from 'vue-router';

const cartStore = useCartStore();
const router = useRouter();
//计算购物车中商品的总数量
const cartCount = computed(() => 
  cartStore.cartProducts.reduce((acc, product) => acc + product.quantity, 0)
);
//切换到购物车页面
function navigateToCart() {
  router.push('/cart');
}
</script>

<template>
  <header class="common-header">
    <div class="logo">
      <h1>你好商城</h1>
    </div>
    <nav>
      <ul>
        <li><router-link to="/">首页</router-link></li>
        <li><router-link to="/cart">购物车({{ cartCount }})</router-link></li>
      </ul>
    </nav>
  </header>
</template>

<style scoped>
.common-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 15px 30px;
  background-color: #42b983;
  color: white;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.common-header .logo h1 {
  font-size: 24px;
  margin: 0;
}

.common-header nav ul {
  list-style: none;
  display: flex;
  margin: 0;
  padding: 0;
}

.common-header nav ul li {
  margin-left: 20px;
}

.common-header nav ul li a {
  color: white;
  text-decoration: none;
  font-weight: bold;
  padding: 5px 10px;
  border-radius: 4px;
  transition: background-color 0.3s;
}

.common-header nav ul li a:hover,
.common-header nav ul li a.router-link-active {
  background-color: rgba(255, 255, 255, 0.2);
}
</style>